package com.polylink.sys.dao.sqlProvider;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.polylink.base.Page;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:53:20
 *
 **/
public class UserSqlProvider {
	private static String user_table = "cti_web_user u";

	public String list(Page p){
		BEGIN();
		SELECT("u.id,u.username name,u.mail,u.phone");
		FROM(user_table);
		conditions(p.getQuery());
		//ORDER_BY(columns);
		
		return SQL()+" limit #{begin},#{pageSize}";
	}
	
	public String count(Page p){
		BEGIN();
		SELECT("count(1)");
		FROM(user_table);
		conditions(p.getQuery());
		return SQL();
	}
	
	public void conditions(Map<String,Object> query){
		String name = (String) query.get("name");
		String mail = (String) query.get("mail");
		String phone = (String) query.get("phone");
		if(StringUtils.isNotBlank(name)){
			query.put("name", "%"+name+"%");
			WHERE("username like #{query.name}");
		}
		if(StringUtils.isNotBlank(mail)){
			query.put("mail", "%"+mail+"%");
			WHERE("mail like #{query.mail}");
		}
		if(StringUtils.isNotBlank(phone)){
			query.put("phone", "%"+phone+"%");
			WHERE("phone like #{query.phone}");
		}
	}
}
